package com.byxy.xyqas.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.byxy.xyqas.dao.UserDao;
import com.byxy.xyqas.entity.User;
import com.byxy.xyqas.service.UserService;
import com.byxy.xyqas.vo.UserVO;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;

	public List<UserVO> findAll() {
		List<UserVO> userVOs = new ArrayList<>();
		List<User> users = userDao.findAll();
		for (User u : users) {
			UserVO userVO = new UserVO();
			BeanUtils.copyProperties(u, userVO);
			userVOs.add(userVO);
		}
		return userVOs;
	}

	public void add(UserVO user) {
		User u = new User();
		BeanUtils.copyProperties(user, u);
		userDao.save(u);
	}

	public UserVO get(Integer userId) {
		User u = userDao.getOne(userId);
		UserVO userVO = new UserVO();
		BeanUtils.copyProperties(u, userVO);
		return userVO;
	}

	public UserVO login(String userName, String userPassword) {
		UserVO userVO = new UserVO();
		User u = userDao.getByUserNameAndUserPassword(userName, userPassword);
		if (u != null) {
			BeanUtils.copyProperties(u, userVO);
			return userVO;
		} else {
			return null;
		}
	}

	public void update(UserVO userVO) {
		User u = new User();
		BeanUtils.copyProperties(userVO, u);
		userDao.save(u);
	}

	public void updateUserStatus(Integer userStatus, Integer userId) {
		User u = userDao.getOne(userId);
		u.setUserStatus(userStatus);
		userDao.save(u);
	}

}
